<?php
class autoClass
{
   public function indexAction()
   {       
       $this->pageInit();
       $body = '
        <nav>
            <a href="'.forward('index').'">项目首页</a>
            <a href="'.url(['p'=>'clone']).'">系统克隆</a>
            <a href="'.url(['p'=>'init']).'">项目初始化</a>
            <a href="'.url(['p'=>'crtpage']).'">生成项目</a>
            <a href="'.url(['p'=>'delpage']).'">删除项目</a>
        </nav>
        ';
        $this->html(array(
            'title' => '首页',
            'body'  => $body
        ));
        //print_r(parseAddr());
   }
   public function initAction()
   {
       //echo '<h4>'.url(['p'=>'index']).'</h4>';
       $body = '
        <nav>
            <a href="JavaScript:history.back(-1);">返回</a>
            <a href="'.url(['p'=>'index']).'">首页</a>
        </nav>
        ';
        $this->html(array(
            'title' => '初始化',
            'body'  => $body
        ));
        $this->pageInit();
        $c = $this->config;
        $dir = $c->dir;
        $arr = ['public/','public/js/','public/css/','public/img/','public/view/'];
        foreach($arr as $v){
            if(!is_dir($dir.'/'.$v)){
                mkdir($dir.'/'.$v);
            } 
        }
   }
   public function cloneAction()
   {
       $this->pageInit();
       $body = '
        <nav>
            <a href="JavaScript:history.back(-1);">返回</a>
            <a href="'.url(['p'=>'index']).'">首页</a>
        </nav>
        ';
        $prj = isset($_POST['prjname'])? $_POST['prjname']:null;
        if($prj){
            $dir = $this->config->dir;
            $des = dirname($dir).'/'.$prj;
            echo $dir.'<hr>'.$des;
            copydir($dir,$des);
        }else{ //  表单
            $body .= '
                <form method="post">
                    <br>名称：<input type="text" name="prjname">
                    <br><button type="submit">提交</button>
                </form>
            ';
        }
        $this->html(array(
            'title' => '克隆',
            'body'  => $body
        ));        
   }
   public function crtpageAction()
   {
       $body = '
            <nav>
                <a href="JavaScript:history.back(-1);">返回</a>
                <a href="'.url(['p'=>'index']).'">首页</a>
            </nav>
        ';        
        $this->pageInit();
        if(isset($_POST['app']) && !empty($_POST['app'])){
            $app = $_POST['app'];
            unset($_POST['app']);
            $dir = $this->config->dir;
            $tpl = [
                '/www/'.$app.'Class.php' =>"
<?php\r\n
class ".$app."Class extends WebAbstract\r\n
{\r\n
\r\n
}\r\n           
                "
            ];
            if(count($_POST)>0){
                $files = array_flip($_POST);
                foreach($files as $k=>$v){
                    unset($files[$k]);
                    $files[$k.$app.'.'.$v] = '';
                }
                $tpl = array_merge($files,$tpl);
            }
            foreach($tpl as $k=>$v){
                if(!is_file($dir.$k)) file_put_contents($dir.$k,$v);
            }
        }elseif(count($_POST)>0){
            $body .= '<h4>表单填写有误！</h4>';
        }else{
            $body .= '
                <form method="post">
                    <br>名称：<input type="text" name="app">
                    <br>js：<input type="checkbox" name="js" value="/public/js/" checked="checked">
                    <br>css：<input type="checkbox" name="css" value="/public/css/" checked="checked">
                    <br>view：<input type="checkbox" name="html" value="/public/view/" checked="checked">
                    <br><button type="submit">生成</button>
                </form>
            ';
        }
        $this->html(array(
            'title' => '生成页面',
            'body'  => $body
        ));
   }
   public function delpageAction()
   {
       $body = '
        <nav>
            <a href="JavaScript:history.back(-1);">返回</a>
            <a href="'.url(['p'=>'index']).'">首页</a>
        </nav>
        ';
        $this->pageInit();
        $dir = $this->config->dir;
        $wbds = scandir($dir.'/www/');
        //  遍历目录
        $webhtml = '';
        foreach($wbds as $v){
            if(substr_count($v,'.php') == '0') continue;
            $name = str_replace('Class.php','',$v);
            $webhtml .= '<li><a href="'.url(['name'=>$name],true).'">'.$name.'</a></li>';
        }
        if($webhtml) $body .= '<div>'.$webhtml.'</div>';
        //  删除
        $name = isset($_GET['name'])? $_GET['name']:null;
        $disable = [
          'index'  
        ];
        if(!empty($name) && !in_array($name,$disable)){
            $tpl = [
                '/public/js/'.$name.'.js',
                '/public/css/'.$name.'.css',
                '/public/view/'.$name.'.html',
                '/www/'.$name.'Class.php'
            ];
            $dels = '';
            foreach($tpl as $v){
                if(is_file($dir.$v) && unlink($dir.$v)){
                    $dels .= '<li>'.$v.'</li>';
                }
            }
            if($dels) $body .= '<p>删除的文件如下：<br>'.$dels.'</p>';
            
        }

        $this->html(array(
            'title' => '删除',
            'body'  => $body
        ));        
   }
   public function html($opt)
   {
       if(!is_array($opt)) return false;
       $arr = ['title','head','body'];
       $tpl = '
        <DOCTYPE html>
        <html>
        <head>
            <title>{title}|系统自动化处理</title>
            {head}
        </head>
        <body>
            {body}
        </body>
        </html>
       ';
       foreach($arr as $v)
       {
           $val = array_key_exists($v,$opt)? $opt[$v]:'';
           $tpl = str_replace('{'.$v.'}',$val,$tpl);
       }
       echo $tpl;
   }
   public function pageInit()
   {
       $this->config = config();
   }
   public $config;
}
header('content-type:text/html;charset=utf-8;');
$p = isset($_GET['p'])? $_GET['p']:'index';
if($p) $p .= 'Action';
if(empty($p)) die('访问参数错误！');
$page = new autoClass();
eval('$page->'.$p.'();');
